package com.myopen.controller.common;

import com.jfinal.aop.Clear;
import com.jfinal.core.ActionKey;
import com.myopen.base.BaseController;
import com.myopen.base.BaseVo;
import com.myopen.model.FileInfo;

public class FileController extends BaseController{

	@Clear
	@ActionKey("/file")
	public void file() {
		String id=getPara("id");
		FileInfo fileInfo=FileInfo.dao.findById(id);
		if(fileInfo!=null) {
			renderFile(fileInfo.getStr("fileName"), fileInfo.getStr("originalFileName"));
		}else {
			renderError(404);
		}
	}
	
	
	@ActionKey("/fileInfo")
	public void fileInfo(){
		BaseVo baseVo=new BaseVo();
		
		String id=getPara("id");
		FileInfo fileInfo=FileInfo.dao.findById(id);
		
		if(fileInfo!=null) {
			baseVo.setCode("0");
			baseVo.setData(fileInfo);
		}else {
			baseVo.setCode("1");
			baseVo.setData("不存在该文件");
		}
		
		renderJson(baseVo);
	}
	
	@Clear
	@ActionKey("/file/upload")
	public void upload(){
		FileInfo fileInfo=addFile("file", null);
		BaseVo baseVo=new BaseVo();
		baseVo.setCode("0");
		baseVo.setData(fileInfo);
		renderJson(baseVo);
	}
	
	@Clear
	@ActionKey("/file/upload/ckeditor")
	public void uploadForCkediter() {
		FileInfo fileInfo=addFile("upload", null);

		String filepath=getRequest().getContextPath()+"/file?id="+fileInfo.getStr("id");
		StringBuffer str=new StringBuffer("");
		String callback=getPara("CKEditorFuncNum");
		str.append("<script type=\"text/javascript\">");
		str.append("window.parent.CKEDITOR.tools.callFunction("+ callback + ",'"+filepath+"','')");
		str.append("</script>");
		renderHtml(str.toString());
	}
	
}
